package yamen.bdwm;

import com.google.analytics.tracking.android.EasyTracker;

import yamen.bdwm.compatible.ActionBarActivity;
import yamen.bdwm.compatible.FragmentWithMenu;
import yamen.bdwm.compatible.FragmentWithMenuPagerAdapter;
import yamen.bdwm.compatible.ViewPagerIndicator;
import yamen.bdwm.compatible.ViewPagerIndicator.PageInfoProvider;
import yamen.bdwm.data.WmMyData;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity {

	public ViewPager pager;
	private PagerAdapter mPagerAdapter;
	MyViewPagerIndicator mVPI;
	boolean menushowed=false;
	int currentFragment;
	public FragmentWithMenu[] fragments;

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		EasyTracker.getInstance().setContext(this);
		EasyTracker.getInstance().activityStart(this);
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance().activityStop(this);
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MainConstant.mainActivity = this;
        MainConstant.nowActivity=this;
       
        View v=findViewById(R.id.menu_more_mask);
        v.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				hidemenu();
			}
		});
        
        
        //add the viewpager
        pager = (ViewPager)findViewById(R.id.pager);
        mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
        pager.setAdapter(mPagerAdapter);
        pager.setCurrentItem(MainConstant.TOPTEN);
        
        //add the pager indicator & the title giver 
        mVPI=new MyViewPagerIndicator();
        ViewPagerIndicator indicator = (ViewPagerIndicator)findViewById(R.id.indicator);
        pager.setOnPageChangeListener(indicator);
        
        
        //init the pager indicator & the title giver 
        indicator.init(MainConstant.TOPTEN, mPagerAdapter.getCount(), mVPI);
        Resources res = getResources();
        Drawable prev = res.getDrawable(R.drawable.indicator_prev_arrow);
        Drawable next = res.getDrawable(R.drawable.indicator_next_arrow);                                                 
        indicator.setArrows(prev, next);
        indicator.setOnClickListener(new OnIndicatorClickListener());
        
		WmMyData.getinstance().parser.getLocalCatalog();
    }
    public Fragment getCurrentFragment()
    {
    	if(pager == null)
    		pager.setCurrentItem(0);
    	return fragments[pager.getCurrentItem()];
    }
    class OnIndicatorClickListener implements ViewPagerIndicator.OnClickListener{
  		public void onCurrentClicked(View v) {
  			;
  		}
  		
  		public void onNextClicked(View v) {
  			pager.setCurrentItem(pager.getCurrentItem()+1);
  		}


  		public void onPreviousClicked(View v) {
  			pager.setCurrentItem(pager.getCurrentItem()-1);
  		}

      	
      }

    class MyViewPagerIndicator implements PageInfoProvider
    {

		public String getTitle(int pos) {
			
			if(pos<4&&pos>=0)return getResources().getStringArray(R.array.indicators)[pos];
			return "";
			
		}
    
    }
    public void onBackPressed ()
    {
    	if(pager.getCurrentItem()==MainConstant.BOARDS&&!((BoardFragment)((PagerAdapter)pager.getAdapter()).getItem(MainConstant.BOARDS)).isTop())
    	{
    		super.onBackPressed();
    	}
    	else
    	{
    		new AlertDialog.Builder(this)
			.setTitle(R.string.exit)
			.setMessage(R.string.exit_confirm)
			.setPositiveButton(R.string.exit, new OnClickListener() {
				
				public void onClick(DialogInterface dialog, int which) {
					if(WmMyData.getinstance().parser.is_login())
					{	
						SetRefreshingDialog(true);
						new AsyncTask<Void, Void, Void>(){

							@Override
							protected Void doInBackground(Void... params) {
								//WmMyData.getinstance().parser.getContent(MainConstant.bbsbase+"bbsout.php?logout=1");
								return null;
							}
							protected void onPostExecute(Void i) {
								SetRefreshingDialog(false);
								finish();
							}
							}.execute();
					}
					else
						{
						
						finish();
						
						}
				}
			} )
			.setNegativeButton(R.string.button_cancel, null).create().show();
    	}
    }
    public void initFragments() {
		// initialize the Fragments
    	Bundle b;
		fragments = new FragmentWithMenu[4];
		for(int i=0;i<4;i++)
		{
			fragments[i] = new BoardFragment();
			b=new Bundle();
		
			b.putInt("type", i);
			fragments[i].setArguments(b);
		}
		/*
		fragments[1] = new TopTenFragment();
		fragments[2] = new BoardFragment();
		fragments[2] = new MyFavFragment();*/
	}

    public void hidemenu()
    {
    	View v=findViewById(R.id.menu_more_mask);
    	v.setVisibility(View.GONE);
		menushowed=false;
    }
    public void showmenu()
    {
    	final FragmentWithMenu f=((PagerAdapter)pager.getAdapter()).getItem(pager.getCurrentItem());
    	ListView lv=(ListView) findViewById(R.id.menu_more_list);
    	
    	lv.setAdapter(f.GetMenu());
    	lv.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				hidemenu();
				f.onMenuClicked(position);
			}
    		
		});
    	View v=findViewById(R.id.menu_more_mask);
    	v.setVisibility(View.VISIBLE);
    	menushowed=true;
    }
    public boolean onOptionsItemSelected (MenuItem item)
    {
    	
    	
    	if(item.getItemId()==R.id.menu_more)
    	{
    		if(menushowed)
    		{
    			hidemenu();
    		}
    		else
    		{
    			showmenu();
    		}
    		return  true;
    	}
    	FragmentWithMenu f=((PagerAdapter)pager.getAdapter()).getItem(pager.getCurrentItem());
		if(! f.onOptionsItemSelected(item, this))
			return super.onOptionsItemSelected(item);
		return true;
	}
    
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.more, menu);
        
        
        
        return super.onCreateOptionsMenu(menu);
    }
    
    public class PagerAdapter extends FragmentWithMenuPagerAdapter {
		public PagerAdapter(FragmentManager fm) {
			super(fm);
		}
		@Override
		public FragmentWithMenu getItem(int pos) {
			if(fragments==null)
			{
				initFragments();
			}
			if(pos<this.getCount())
				return fragments[pos];
			return null;
				
		}

		@Override
		public int getCount() {
			return 4;
		}
		
    }
}